/**
 * 用户功能模块 Composable
 * 封装用户页面的各种功能逻辑：意见反馈、打卡记录等
 */

import { UserService } from '@/api/user';

export interface SuggestionItem {
  credate: string;
  suggestionid: string;
  advicemsg: string;
  creId: string;
  creName: string;
}

export interface ClickInItem {
  credate: string;
  cciId: string;
  address: string;
  longitude: string;
  latitude: string;
  creName: string;
}

export interface UserMenuItem {
  keyword: string;
  pmsName: string;
  icon: string;
}

/**
 * 使用意见反馈功能
 */
export function useSuggestionFeature() {
  const suggestionShow = ref(false);
  const suggestMsg = ref('');
  const suggestMsgShow = ref(false);

  /**
   * 提交意见反馈
   */
  const commitSuggestion = async () => {
    if (!suggestMsg.value) {
      showError('请输入内容');
      return;
    }

    const params = {
      advicemsg: suggestMsg.value,
    };
    await UserService.insertSuggestion(params);
    suggestionShow.value = false;
    suggestMsg.value = '';
    showSuccess('提交成功');
  };

  /**
   * 获取意见反馈列表
   */
  const fetchSuggestionList = async (params: { pageNum: number; pageSize: number }) => {
    const response = await UserService.querySuggestion(params);
    return {
      list: response.list,
      hasNextPage: response.hasNextPage,
      total: response.total,
    };
  };

  /**
   * 打开意见反馈弹框
   */
  const openSuggestionModal = () => {
    suggestionShow.value = true;
  };

  /**
   * 打开意见反馈列表
   */
  const openSuggestionList = () => {
    suggestMsgShow.value = true;
  };

  return {
    // 状态
    suggestionShow,
    suggestMsg,
    suggestMsgShow,

    // 方法
    commitSuggestion,
    fetchSuggestionList,
    openSuggestionModal,
    openSuggestionList,
  };
}

/**
 * 使用打卡记录功能
 */
export function useClickInFeature() {
  const clickInShow = ref(false);
  const keywords = ref('');
  const result = ref([]);
  const sureResult = ref([]);

  /**
   * 获取打卡记录列表（这里需要根据实际API实现）
   */
  const fetchClickInList = async (params: { pageNum: number; pageSize: number }) => {
    // 这里应该调用真实的打卡记录API
    // 暂时返回空数据结构
    return {
      list: [],
      hasNextPage: false,
      total: 0,
    };
  };

  /**
   * 打开打卡记录列表
   */
  const openClickInList = () => {
    keywords.value = '';
    result.value = [];
    sureResult.value = [];
    clickInShow.value = true;
  };

  return {
    // 状态
    clickInShow,
    keywords,
    result,
    sureResult,

    // 方法
    fetchClickInList,
    openClickInList,
  };
}

/**
 * 使用用户菜单功能
 */
export function useUserMenu() {
  const userMenu = ref<UserMenuItem[]>([]);
  const suggestionFeature = useSuggestionFeature();
  const clickInFeature = useClickInFeature();

  /**
   * 处理菜单点击事件
   */
  const handleMenuClick = (item: UserMenuItem) => {
    switch (item.keyword) {
      case 'insertSuggest':
        suggestionFeature.openSuggestionModal();
        break;
      case 'querySuggest':
        suggestionFeature.openSuggestionList();
        break;
      case 'getClickIn':
        clickInFeature.openClickInList();
        break;
      default:
        console.warn('未知的菜单操作:', item.keyword);
        break;
    }
  };

  /**
   * 初始化用户菜单
   */
  const initUserMenu = (menuData: any) => {
    const menuArr = menuData.wd || {};
    userMenu.value = menuArr[440001] || [];
  };

  return {
    // 状态
    userMenu: readonly(userMenu),

    // 方法
    handleMenuClick,
    initUserMenu,

    // 功能模块
    suggestionFeature,
    clickInFeature,
  };
}

/**
 * 使用用户信息
 */
export function useUserInfo() {
  const user = ref<{
    userId: string;
    userName: string;
    depName: string;
    headerImg: string;
  }>({
    userId: '',
    userName: '',
    depName: '',
    headerImg: '',
  });

  /**
   * 初始化用户信息
   */
  const initUserInfo = (userInfo: any, domainUrl: string) => {
    const { userId, userName, depName, headImgUrl } = userInfo;
    user.value = {
      userId,
      userName,
      depName,
      headerImg: `${domainUrl}${headImgUrl}`,
    };
  };

  return {
    user: readonly(user),
    initUserInfo,
  };
}
